﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Publishing;
using System.Data;
using Microsoft.SharePoint;

namespace AgileShare.Framework.CamlQueries
{
	/// <summary>
	/// 
	/// </summary>
	public class CrossListQueryHelper
	{
		/// <summary>
		/// Gets the items.
		/// </summary>
		/// <param name="queryInfo">The query info.</param>
		/// <param name="site">The site.</param>
		/// <returns></returns>
		public static DataTable GetItems(CrossListQueryInfo queryInfo, SPSite site)
		{
			CrossListQueryCache cache = new CrossListQueryCache(queryInfo);
			DataTable results = cache.GetSiteData(site);
			return results;
		}

		/// <summary>
		/// Gets the items.
		/// </summary>
		/// <param name="lists">The lists.</param>
		/// <param name="webScope">The web scope.</param>
		/// <param name="camlQueryBuilder">The caml query builder.</param>
		/// <param name="site">The site.</param>
		/// <param name="useCache">if set to <c>true</c> [use cache].</param>
		/// <param name="rowLimit">The row limit.</param>
		/// <returns></returns>
		public static DataTable GetItems(QueryLists lists, WebScope webScope, CamlQueryBuilder camlQueryBuilder, SPSite site, bool useCache = true, uint? rowLimit = null)
		{
			var queryBuilder = new CrossListQueryBuilder(lists, webScope, camlQueryBuilder, useCache, rowLimit);
			var queryInfo = queryBuilder.BuildCrossListQueryInfo();

			var items = GetItems(queryInfo, site);

			return items;
		}

		/// <summary>
		/// Gets the items.
		/// </summary>
		/// <param name="serverTemplate">The server template.</param>
		/// <param name="webScope">The web scope.</param>
		/// <param name="camlQueryBuilder">The caml query builder.</param>
		/// <param name="site">The site.</param>
		/// <param name="useCache">if set to <c>true</c> [use cache].</param>
		/// <param name="rowLimit">The row limit.</param>
		/// <returns></returns>
		public static DataTable GetItems(int serverTemplate, WebScope webScope, CamlQueryBuilder camlQueryBuilder, SPSite site, bool useCache = true, uint? rowLimit = null)
		{
			var queryBuilder = new CrossListQueryBuilder(serverTemplate, webScope, camlQueryBuilder, useCache, rowLimit);
			var queryInfo = queryBuilder.BuildCrossListQueryInfo();

			var items = GetItems(queryInfo, site);

			return items;
		}

		/// <summary>
		/// Gets the items.
		/// </summary>
		/// <param name="baseType">Type of the base.</param>
		/// <param name="webScope">The web scope.</param>
		/// <param name="camlQueryBuilder">The caml query builder.</param>
		/// <param name="site">The site.</param>
		/// <param name="useCache">if set to <c>true</c> [use cache].</param>
		/// <param name="rowLimit">The row limit.</param>
		/// <returns></returns>
		public static DataTable GetItems(ListBaseType baseType, WebScope webScope, CamlQueryBuilder camlQueryBuilder, SPSite site, bool useCache = true, uint? rowLimit = null)
		{
			var queryBuilder = new CrossListQueryBuilder(baseType, webScope, camlQueryBuilder, useCache, rowLimit);
			var queryInfo = queryBuilder.BuildCrossListQueryInfo();

			var items = GetItems(queryInfo, site);

			return items;
		}
	}
}
